﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

namespace GloberoSport.Negocio
{
    public class Utilidades
    {
        public static int obtenerDatoInt(string sTextBox)
        {
            try
            {
                if (sTextBox.Trim().Length > 0)
                    return Convert.ToInt32(sTextBox);
                else
                    return -10;
            }
            catch (Exception ex)
            {
                return int.MinValue;
            }
        }
        public static DateTime obtenerDatoDateTime(string sTextBox)
        {
            try
            {
                if (sTextBox.Trim().Length > 0)
                    return Convert.ToDateTime(sTextBox);
                else
                    return new DateTime(1901, 1, 1);
            }
            catch (Exception ex)
            {
                return new DateTime(1901, 1, 1);
            }
        }
        public static string obtenerDato(DataRow r, string sPropiedad)
        {
            try
            {
                return r[sPropiedad].ToString();
            }
            catch (Exception ex)
            {
                return "";
            }
        }
        public static int obtenerDatoInt(DataRow r, string sPropiedad)
        {
            try
            {
                return Convert.ToInt32(r[sPropiedad].ToString());
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        public static double obtenerDatoDouble(DataRow r, string sPropiedad)
        {
            try
            {
                return Convert.ToDouble(r[sPropiedad].ToString());
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        public static float obtenerDatoFloat(DataRow r, string sPropiedad)
        {
            try
            {
                return Convert.ToSingle(r[sPropiedad].ToString());
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        public static Decimal obtenerDatoDecimal(DataRow r, string sPropiedad)
        {
            try
            {
                return Convert.ToDecimal(r[sPropiedad].ToString());
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        public static DateTime obtenerDatoDateTime(DataRow r, string sPropiedad)
        {
            try
            {
                return Convert.ToDateTime(r[sPropiedad].ToString());
            }
            catch (Exception ex)
            {
                return new DateTime(1901, 1, 1);
            }
        }
        public static bool obtenerDatoBoolean(DataRow r, string sPropiedad)
        {
            try
            {
                return Convert.ToBoolean(r[sPropiedad].ToString());
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //PROCESAR DATOS
        public static void procesarDataSet()
        {
        }

        public static decimal CalculateDistanceBetweenGPSCoordinates(decimal _lon1, decimal _lat1, decimal _lon2, decimal _lat2)
        {
            double lon1 = (double)_lon1;
            double lon2 = (double)_lon2;
            double lat1 = (double)_lat1;
            double lat2 = (double)_lat2;
            const double R = 3956;
            const double degreesToRadians = Math.PI / 180;

            //convert from fractional degrees (GPS) to radians
            lon1 *= degreesToRadians;
            lat1 *= degreesToRadians;
            lon2 *= degreesToRadians;
            lat2 *= degreesToRadians;

            double dlon = lon2 - lon1;
            double dlat = lat2 - lat1;
            double a = Math.Pow(Math.Sin(dlat / 2), 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow(Math.Sin(dlon / 2), 2);
            double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            double d = R * c;
            d = d * 1.852;

            return (decimal)d;
        }
    }
}

